package com.xtgg.baby;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class BabyDeviceNumberActivity extends BaseActivity implements
		OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_device_number);

		((TextView) findViewById(R.id.center)).setText("设备序列号");

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.login).setOnClickListener(this);
		
		((TextView) findViewById(R.id.number)).setText(getIntent().getStringExtra("serial"));
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;

		case R.id.login:
	/*		String number = ((EditText) findViewById(R.id.number)).getText()
					.toString();
			if (number.length() == 0) {
				ToastUtil.showToastWithAlertPic("请输入序列号");
				return;
			}*/
			sendBoundHttpRequest();
			break;
		}
	}
	
	private void sendBoundHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("state").equals("1")) {
					JSONObject obj = ruser.getJSONArray("data").getJSONObject(0);
					
					if(obj.getString("andriod_code").length() == 0)
					{
						sendCheckHttpRequest(obj.getString("deviceid"));
					}
					else
					{
						Intent intent = new Intent(BabyDeviceNumberActivity.this, BabyBoundListActivity.class);
						intent.putExtra("info", getIntent().getStringExtra("info"));
						intent.putExtra("deviceid", obj.getString("deviceid"));
						startActivity(intent);
						
						finish();
					}
				}
				else if (ruser.getString("state").equals("2"))
				{
					showExitDialog();
				} else {
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
		};

		JSONObject obj = JSON.parseObject(getIntent().getStringExtra("info"));
		
		RequestParams params = new RequestParams();
		params.put("act", "connectNewDevice");
		params.put("userid", PreferencesUtils.getString(this, "userid"));
		params.put("devicenumber", ((TextView) findViewById(R.id.number)).getText()
				.toString());
		params.put("andriod_code", obj.getString("address"));
		params.put("devicemodel", getIntent().getStringExtra("model"));
		params.put("password", "111111");
		params.put("nickname", obj.getString("name"));
		params.put("longitude", ((DemoApp)getApplicationContext()).mLng);
		params.put("latitude", ((DemoApp)getApplicationContext()).mLat);
		params.put("power", String.valueOf(getIntent().getExtras().getInt("batty")));

		MgqRestClient.get(Constants.DEVICEMGT, params, loginHandler);
	}
	
	private void sendCheckHttpRequest(final String deviceId) {
		MgqDataHandler loginHandler = new MgqDataHandler(this,
				true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					Intent intent = new Intent(BabyDeviceNumberActivity.this, BabyBoundListActivity.class);
					intent.putExtra("info", getIntent().getStringExtra("info"));
					intent.putExtra("deviceid", deviceId);
					startActivity(intent);
					
					finish();
				} 
				else if (ruser.getState().equals("2"))
				{
					showExitDialog();
				}
				else 
				{
					
				}
			}
		};

		JSONObject obj = JSON.parseObject(getIntent().getStringExtra("info"));
		RequestParams params = new RequestParams();
		params.put("act", "updateDeviceInfo");
		params.put("deviceid", deviceId);
		params.put("andriod_code", obj.getString("address"));
		params.put("ios_code", obj.getString("ios_code"));

		MgqRestClient.get(Constants.SETTING, params, loginHandler);
	}
}
